﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace BabyShop.CP
{
    public partial class edituser : System.Web.UI.Page
    {
        DataTable user = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request["id"] != null && (int.Parse(Request["id"])) > 0)
            {
                user = DAO.User.getuser(int.Parse(Request["id"]));
            }
            else Response.Redirect("~/CP/User.aspx");
            if (!IsPostBack)
            {
                //Label1.Text = Request["id"].ToString();
                if (user.Rows.Count > 0)
                {
                    txt_username.Text = user.Rows[0][1].ToString();
                    txt_email.Text = user.Rows[0][3].ToString();
                    txt_fullname.Text = user.Rows[0][4].ToString();
                    txt_address.Text = user.Rows[0][5].ToString();
                    datepicker.Text = user.Rows[0][6].ToString();
                }
            }
        }

        protected void cmd_update_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_password.Text) && string.IsNullOrEmpty(txt_repassword.Text))
            {
                if (txt_username.Text == user.Rows[0][1].ToString() && txt_email.Text == user.Rows[0][3].ToString() && txt_fullname.Text == user.Rows[0][4].ToString() && txt_address.Text == user.Rows[0][5].ToString() && datepicker.Text == user.Rows[0][6].ToString())
                {
                    Response.Redirect("~/CP/User.aspx");
                }
                else
                {
                    DBO.User us = new DBO.User();
                    us.ID = int.Parse(Request["id"]);
                    us.password = user.Rows[0][2].ToString();
                    us.email = txt_email.Text;
                    us.fullname = txt_fullname.Text;
                    us.address = txt_address.Text;
                    us.birthday = datepicker.Text;
                    DAO.User.Update(us);
                    Response.Redirect("~/CP/User.aspx");
                }
            }
            else
            {
                if (txt_password.Text.Equals(txt_repassword.Text))
                {
                    DBO.User us = new DBO.User();
                    us.ID = int.Parse(Request["id"]);
                    us.password = txt_password.Text;
                    us.email = txt_email.Text;
                    us.fullname = txt_fullname.Text;
                    us.address = txt_address.Text;
                    us.birthday = datepicker.Text;
                    DAO.User.Update(us);
                    Response.Redirect("~/CP/User.aspx");
                }
                else
                {
                    txt_password.BorderColor = System.Drawing.Color.Red;
                    txt_repassword.BorderColor = System.Drawing.Color.Red;
                    lb_password.Text = "Hai mật khẩu không giống nhau.";
                }
            }
        }
    }
}